<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealId">
            <router-link
              target="_blank"
              :to="{path:'/deal/home/dealBaseHome',query:{id:form.dealId,companyId:form.companyId}}"
              class="link_a"
            >{{ form.dealName }}
            </router-link>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司名称')" prop="companyId">
            <router-link
              target="_blank"
              :to="{path:'/company/base/companyBaseHome',query:{companyId:form.companyId}}"
              class="link_a"
            >{{ form.companyName }}
            </router-link>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" :placeholder="$t('申请标题')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="comment">
            <el-input v-model="form.comment" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="area-header">
        <span class="area-header-title">{{ $t('团队成员') }}</span>
      </div>
      <el-table
        :data="form.dealHxsTeamList"
        border
        class="nowrapTable formTable"
      >
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('用户名称')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('用户名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealHxsTeamList.' + scope.$index + '.userId' " :rules="dealTeamRules.userId">
              <UserSelect v-model="scope.row.userId" :popper-append-to-body="true" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('参与角色')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('参与角色') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealHxsTeamList.' + scope.$index + '.role' " :rules="dealTeamRules.role">
              <dictSelect
                v-model="scope.row.role"
                :clearable="true"
                dictionary="deal_team_role"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('参与时间')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('参与时间') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealHxsTeamList.' + scope.$index + '.joinTime' ">
              <el-date-picker
                v-model="scope.row.joinTime"
                clearable
                style="width: 200px"
                type="date"
                value-format="yyyy-MM-dd"
                :placeholder="$t('参与时间')"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('是否离开')" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'dealHxsTeamList.' + scope.$index + '.leave' " :rules="dealTeamRules.leave">
              <el-radio-group v-model="scope.row.leave">
                <el-radio
                  v-for="dict in leaveOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{ $t(dict.dictLabel) }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="80">
          <template slot-scope="scope">
            <el-button size="small" type="text" icon="el-icon-delete" @click="delHandle(scope.$index)" />
          </template>
        </el-table-column>
      </el-table>
      <div v-if="!otherBtnHide" class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addHandle">{{ $t("添加") }}</el-button>
      </div>
      <FileTable :list.sync="form.fileList" data-type="deal_hxs_process" :btn-show="!otherBtnHide" />
    </el-form>
  </div>
</template>

<script>
    import { getDealInitialize } from '@/api/sass/hxs/dealHxsWfStorage'
    import { getDealHxsApplicationApply, saveDealHxsApplicationApply } from '@/api/sass/hxs/dealHxsApplicationApply'
    export default {
      components: {
      },
        data() {
            return {
              // 是否离开选项
              leaveOptions: [],
              // 聚焦行业一级选项
              industryFocusOptions: [],
              // 聚焦行业二级选项
              industryFocusTwoOptions: [],
              // 是否隐藏其它按钮
              otherBtnHide: false,
                // 表单参数
                form: {
                  dealHxsTeamList: [],
                  fileList: []
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  actTitle: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ]
                },
              dealTeamRules: {
                userId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                role: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                joinTime: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
              }
            }
        },
        created() {
          this.$bus.$emit('async_done')
          this.getDicts('sys_yes_no').then(response => {
            this.leaveOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                  getDealHxsApplicationApply(data).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                  getDealInitialize(this.$route.query.id).then(response => {
                    this.form.dealName = response.data.dealName
                    this.form.companyName = response.data.companyName
                    this.form.dealHxsTeamList = response.data.dealHxsTeamList
                    if (data != null) {
                      this.form.fileList = data.fileList
                    }
                      this.loading = false
                  })
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    companyId: this.$route.query.companyId,
                    actTitle: undefined,
                    comment: undefined,
                    delFlag: undefined,
                    createTime: undefined,
                    createBy: undefined,
                    updateTime: undefined,
                    updateBy: undefined,
                  dealHxsTeamList: [],
                  fileList: []
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
          editForm() {
            this.formOpen = true
            this.$nextTick(() => {
              this.$refs.dealBaseForm.openLocation = 'bfHome'
              this.$refs.dealBaseForm.init(this.$route.query)
            })
          },
          /** 提交按钮 */
          submitForm: function() {
            return new Promise((resolve, reject) => {
              this.$refs['form'].validate(valid => {
                if (valid) {
                  this.loading = true
                  saveDealHxsApplicationApply(this.form).then(response => {
                    if (response.code === 200) {
                      this.msgSuccess(this.$t('保存成功'))
                      this.loading = false
                      resolve(response.data.id)
                    } else {
                      this.msgError(response.msg)
                      this.loading = false
                      reject(response)
                    }
                  }).catch(() => {
                    this.loading = false
                  })
                } else {
                  this.locationError(document)
                  reject()
                }
              })
            })
          },
          // 删除团队成员
          delHandle(index) {
              this.form.dealHxsTeamList.splice(index, 1)
          },
          // 添加团队成员
          addHandle() {
            const row = {
              id: undefined,
              user: undefined,
              dealId: this.$route.query.id,
              userId: undefined,
              role: undefined,
              joinTime: new Date(),
              leave: 'N',
              leaveTime: undefined,
              remark: undefined
            }
            if (!(this.form.dealHxsTeamList instanceof Array)) {
              this.$set(this.form, 'dealHxsTeamList', [])
            }
            this.$nextTick(() => {
              this.form.dealHxsTeamList.push(row)
            })
          }
        }
    }
</script>
